const hljs = require('highlight.js');
const { escapeHtml } = require('markdown-it/lib/common/utils');

module.exports = (str, lang) => {
  if (lang && hljs.getLanguage(lang)) {
    try {
      return `<pre class="hljs"><code>${
        hljs.highlight(str, { language: lang, ignoreIllegals: true }).value
      }</code></pre>`;
    } catch (err) {
      console.log(err);
    }
  }
  return `<pre class="hljs"><code>${escapeHtml(str)}</code></pre>`;
};
